package cloud.maque.admin.aop;

import cloud.maque.admin.domain.vo.MqCreateUserModel;
import cloud.maque.framework.security.ssoclient.utils.SsoUserInfoUtils;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.util.FieldUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;

/**
 * 自动给数据添加租户ID
 *
 * @author zhuyf
 * @date 2023/3/21 20:14
 */
@Aspect
@Component
@Slf4j
public class DataAop {


    @Autowired
    SsoUserInfoUtils infoUtils;

    @Pointcut("execution(* cloud.maque.admin.controller..*(..))")
    public void controller() {

    }

    /**
     * 租户ID 根据用户当前的Token去修改租户ID,防止数据被篡改
     *
     * @param point
     * @throws IllegalAccessException
     */
    @Before("controller()")
    public void Before(JoinPoint point) throws IllegalAccessException {

        //从会话中获取租户ID

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getMethod().equalsIgnoreCase(RequestMethod.POST.name())) {

            Object[] arguments = point.getArgs();
            for (Integer i = 0; i < arguments.length; i++) {
                if (arguments[i] != null && arguments[i].getClass().getName().contains("maque")) {
                    Class classBiz = arguments[i].getClass();
                    try {
                        Field field1 = FieldUtils.getField(classBiz, "tenantId");
                        if (field1 != null) {
                            field1.setAccessible(true);
                            if (StrUtil.isEmptyIfStr(field1.get(arguments[i]))) {
                                //替换请求的租户ID
                                String tenantId = infoUtils.getTenantId();
                                field1.set(arguments[i], tenantId);
                            }
                        }
                    } catch (IllegalStateException ex) {
                        //没匹配到字段就不处理
                    }

                }
            }
        }
    }


    /**
     * 切入用户保存方法
     */
    @Pointcut("execution(* cloud.maque.admin.service.MqUserService.saveUser*(..))")
    public void SaveUser() {

    }

    @Before("controller()")
    public void SaveUser(JoinPoint point) {

        Object[] arguments = point.getArgs();
        for (Integer i = 0; i < arguments.length; i++) {
            if (arguments[i] != null && arguments[i] instanceof MqCreateUserModel) {
                MqCreateUserModel model = (MqCreateUserModel) arguments[i];
                if (StrUtil.isEmptyIfStr(model.getUser().getTenantId())) {
                    String tenantId = infoUtils.getTenantId();
                    model.getUser().setTenantId(tenantId);
                }
            }
        }

    }

}
